package dji.ux.ext.panel.camera;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.common.bus.UXSDKEventBus;
import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.thirdparty.rx.subscriptions.CompositeSubscription;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;
import dji.ux.ext.base.UXEEvents;

public class CameraFunctionPanel extends ConstraintLayoutWidget {

    private CameraFunctionModel widgetModel;
    private CompositeSubscription subscription = new CompositeSubscription();
    private CameraH20TPanel h20TPanel;
    private CameraMavic2DualPanel mavic2DualPanel;

    public CameraFunctionPanel(@NonNull Context context) {
        super(context);
    }

    public CameraFunctionPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CameraFunctionPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        if (!isInEditMode()) {
            widgetModel = new CameraFunctionModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance());
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            widgetModel.setup();
        }

        this.subscription.add(UXSDKEventBus.getInstance().
                register(UXEEvents.RemoteCameraMode.class)
                .observeOn(dji.thirdparty.rx.android.schedulers.AndroidSchedulers.mainThread())
                .subscribe(this::onRemoteCameraMode));
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            widgetModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(widgetModel.getProductConnection()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::isProductConnected));
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }

    private void isProductConnected(Boolean isConnected) {
        if (isConnected != null && isConnected) {
            setVisibility(VISIBLE);
            if (widgetModel.isH20TCamera()) {
                h20TPanel = new CameraH20TPanel(getContext());
                addView(h20TPanel);
            } else if (widgetModel.isMavic2DualCamera()) {
                mavic2DualPanel = new CameraMavic2DualPanel(getContext());
                addView(mavic2DualPanel);
            } else {
                removeAllViews();
                setVisibility(GONE);
            }
        } else {
            removeAllViews();
            setVisibility(GONE);
        }
    }

    private void onRemoteCameraMode(UXEEvents.RemoteCameraMode mode) {
        if (widgetModel.isH20TCamera()) {
            if (h20TPanel != null)
                h20TPanel.onRemoteMode(mode);
        } else if (widgetModel.isMavic2DualCamera()) {
            if (mavic2DualPanel != null)
                mavic2DualPanel.onRemoteMode(mode);
        }
    }
}
